/**
 * 
 */
package slhs.tempo.scriptGenerator;

import java.awt.LayoutManager;
import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.mlc.swing.layout.LayoutConstraintsManager;

import slhs.tempo.TableAnalyzer.IntegerField;
import slhs.tempo.dataImporterUtilities.IOFileHandler;

/**
 * AudioFeedbackPanel.java
 * 
 * @author Swapnil Devikar
 * 
 */
public class AudioFeedbackPanel extends JPanel implements PanelHandler {

    private static final AudioFeedbackPanel audioFeedbackPanel = new AudioFeedbackPanel();

    private static final String AUDIO_FEEDBACK_OPTIONS_PANEL_LAYOUT = "AUDIO_FEEDBACK_OPTIONS_PANEL_LAYOUT.XML";

    private static final String AUDIO_FEEDBACK = "2";

    private JPanel audioFeedbackOptionsPanel;

    private JTextField wrongAnswerFeedbackAudioTextField;
    private JTextField correctAnswerFeedbackAudioTextField;
    private JTextField durationTextField;

    private JButton wrongAnswerFeedbackAudioChooseButton;
    private JButton correctAnswerFeedbackAudioChooseButton;

    private JLabel correctAnswerFeedbackAudioLabel;
    private JLabel durationLabel;
    private JLabel wrongAnswerFeedbackAudioLabel;

    private AudioFeedbackPanel() {
	initComponents();
	addComponents();
    }

    /**
     * 
     */
    private void initComponents() {
	// here are declarations for the controls you created
	audioFeedbackOptionsPanel = new JPanel();
	wrongAnswerFeedbackAudioTextField = new JTextField();
	correctAnswerFeedbackAudioLabel = new JLabel("Correct Answer Feedback Audio");
	wrongAnswerFeedbackAudioChooseButton = new JButton("Choose");
	durationLabel = new JLabel("Duration On Screen (sec.)");
	correctAnswerFeedbackAudioTextField = new JTextField();
	wrongAnswerFeedbackAudioLabel = new JLabel("Wrong Answer Feedback Audio");
	correctAnswerFeedbackAudioChooseButton = new JButton("Choose");
	durationTextField = new IntegerField();
    }

    /**
     * 
     */
    private void addComponents() {
	LayoutConstraintsManager layoutConstraintsManager = LayoutConstraintsManager.getLayoutConstraintsManager(this
		.getClass().getResourceAsStream(AUDIO_FEEDBACK_OPTIONS_PANEL_LAYOUT));
	LayoutManager panelLayout = layoutConstraintsManager.createLayout("panel", this);
	audioFeedbackOptionsPanel.setLayout(panelLayout);

	audioFeedbackOptionsPanel.add(wrongAnswerFeedbackAudioTextField, "wrongAnswerFeedbackAudioTextField");
	audioFeedbackOptionsPanel.add(correctAnswerFeedbackAudioLabel, "correctAnswerFeedbackAudioLabel");
	audioFeedbackOptionsPanel.add(wrongAnswerFeedbackAudioChooseButton, "wrongAnswerFeedbackAudioChooseButton");
	audioFeedbackOptionsPanel.add(durationLabel, "durationLabel");
	audioFeedbackOptionsPanel.add(correctAnswerFeedbackAudioTextField, "correctAnswerFeedbackAudio");
	audioFeedbackOptionsPanel.add(wrongAnswerFeedbackAudioLabel, "wrongAnswerFeedbackAudioLabel");
	audioFeedbackOptionsPanel.add(correctAnswerFeedbackAudioChooseButton, "correctAnswerFeedbackAudioChooseButton");
	audioFeedbackOptionsPanel.add(durationTextField, "durationTextField");

	// add action listeners
	correctAnswerFeedbackAudioChooseButton.addActionListener(this);
	wrongAnswerFeedbackAudioChooseButton.addActionListener(this);

    }

    /*
     * (non-Javadoc)
     * 
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    @Override
    public void actionPerformed(ActionEvent event) {

	Object source = event.getSource();
	FileNameExtensionFilter filter = new FileNameExtensionFilter("Audio Files", "wav", "mp3");
	String[] filePath = new IOFileHandler().selectFile(true, filter);
	if (source.equals(correctAnswerFeedbackAudioChooseButton)) {
	    correctAnswerFeedbackAudioTextField.setText(filePath[0]);
	} else if (source.equals(wrongAnswerFeedbackAudioChooseButton)) {
	    wrongAnswerFeedbackAudioTextField.setText(filePath[0]);
	}
    }

    /*
     * (non-Javadoc)
     * 
     * @see slhs.tempo.scriptGenerator.PanelHandler#getPanel()
     */
    @Override
    public JPanel getPanel() {
	return audioFeedbackOptionsPanel;
    }

    /**
     * @return
     */
    public static AudioFeedbackPanel getInstance() {
	return audioFeedbackPanel;
    }

    public String getFeedbackString() {
	StringBuffer audioFeedbackString = new StringBuffer();

	// append 2 for audio feedback
	audioFeedbackString.append(AUDIO_FEEDBACK);
	audioFeedbackString.append(UtilityConstants.VALUES_DELIMITER);

	// correct answer audio feedback
	audioFeedbackString.append(getCorrectanswerAudioFeedback());
	audioFeedbackString.append(UtilityConstants.VALUES_DELIMITER);

	// wrong answer audio feedback
	audioFeedbackString.append(getWrongAnswerAudioFeedback());

	return audioFeedbackString.toString();
    }

    /**
     * @return
     */
    private Object getWrongAnswerAudioFeedback() {
	return wrongAnswerFeedbackAudioTextField.getText();
    }

    /**
     * @return
     */
    private String getCorrectanswerAudioFeedback() {
	return correctAnswerFeedbackAudioTextField.getText();
    }

}
